package com.galengao.springboot.eventlistener.listener;

import com.galengao.springboot.eventlistener.event.CouponEvent;
import com.galengao.springboot.eventlistener.event.MessageEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

/**
 * 通用Listener
 *
 * @author galenGao
 * @version Id: CommonListener.java, v 0.1 2022/7/25 22:20  GalenGao Exp $$
 */
@Component
@Slf4j
public class CommonListener {


    @EventListener
    public void sendMsg(MessageEvent messageEvent){
        log.info("发送短信给：{}，消息内容：{}",messageEvent.getUsername(),messageEvent.getMessage());
        // TODO 此处省略发送发送短信的逻辑
    }

    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
    @Async
    public void addCoupon(CouponEvent couponEvent){
        log.info("给用户：{}，添加代金券金额：{}",couponEvent.getUsername(),couponEvent.getBalance());
        // TODO 此处省略添加代金券的逻辑
        try {
            Thread.sleep(7000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
